import QtQuick 2.12
//import QtQuick.Controls 1.4
import QtQuick.Controls 2.7
import QtQuick.Layouts 1.12

Page {
    id: page3
    visible: false


    header:Rectangle {
        height: 64
        color: "green"
        RowLayout {
            anchors.fill: parent
            Button {
                id: retbtn
                width: 60
                height: 48
                background: Item {

                }
                onClicked:  {
                    //page2.StackView.view.pop() //not work
                    //console.log("stackView: " + stackView)
                    //console.log("page2.StackView.view: " + page2.StackView.view)
                    stackView.pop()
                }
                icon {
                    source: "../images/icon_back.png"
                    color: "white"
                    height: 36
                    width: 36
                }
            }

            Text {
                text: qsTr("广播设置")
                font.pixelSize: 22
            }
            Item {
                Layout.fillWidth: true
            }
            CurrentDateTime {
                Layout.rightMargin: 10
            }


        }
    }

    Button {
        id: addBCFileBtn
        text: qsTr("添加广播文件")
        background:  Rectangle {
            color: "green"
        }
        anchors.right: parent.right
        anchors.topMargin: 20
        anchors.rightMargin: 20
        anchors.top: parent.top
        onClicked: {
            addBCFilePopup.open()

        }

    }
    ListView{
        id: bcfview
        Component.onCompleted: {
            model = Services.getBroadcastFileNames();
        }
        anchors {
            top: addBCFileBtn.bottom
            bottom: parent.bottom
            topMargin: 32
        }
        width: parent.width
        height: 100

        delegate:Flickable {
            id: fk
            width: parent.width;
            height: 48
            contentWidth: parent.width+delbtn.width;
            contentHeight: 48
            boundsBehavior:Flickable.StopAtBounds //该属性设置过后，边界不会被拉出

            PropertyAnimation{
                id: btnShow
                target: fk
                property: "contentX"
                duration: 100

                to: delbtn.width
            }
            PropertyAnimation{
                id: btnHide
                target: fk
                property: "contentX"
                duration: 100
                to: 0
            }
            onMovementEnded: {

                if(fk.contentX > delbtn.width/2) {
                    btnShow.start()
                }else {
                    btnHide.start()
                }


            }

            Rectangle {
                anchors.fill: parent
                color: "#e5ffe5"
                border.width: 1
                border.color: "#eee"
                Text {
                    id: fname
                    //anchors.centerIn: parent
                    font.pointSize: 16
                    anchors.left: parent.left
                    anchors.leftMargin: 64
                    anchors.verticalCenter: parent.verticalCenter
                    text: modelData.split("/")[1]


                }

                Rectangle{
                    id: delbtn
                    height: parent.height
                    //width: parent.width*0.28
                    width: 100
                    color: "red"

                    anchors.right: parent.right

                    Text {

                        anchors.centerIn: parent

                        text: qsTr("删除")

                        color: "#ffffff"

                    }

                    MouseArea{

                        anchors.fill: parent

                        onClicked: {
                            Services.delBroadCastFileName(fname.text)
                            bcfview.model = Services.getBroadcastFileNames();
                        }

                    }

                }
            }

        }
    }
    Popup {
        id: addBCFilePopup
        parent: Overlay.overlay
        anchors.centerIn: parent
        dim: true
        background: Rectangle {

        }
        onClosed: {
            cnt_timer.running = false
            cnt_timer.seconds = 0;
            curtime.text = "00:00"
            filename.clear()
            notify.opacity = 0
            bkd.color = "green"
            recBtn.text = qsTr("开始采集")
        }
        padding: 0
        height: 400
        width: 600
        Rectangle {
            id: popHeader
            width: parent.width
            height: 64
            color: "green"
            Text {
                text: qsTr("采集广播文件")
                font.pixelSize: 24
                anchors {
                    left: parent.left
                    leftMargin: 20
                    verticalCenter: parent.verticalCenter
                }
                color: "white"
            }
        }
        ColumnLayout {
            anchors {
                left: parent.left
                right: parent.right
                bottom: parent.bottom
                margins: 20
            }
            anchors.top: popHeader.bottom
            TextField {
                id: filename
                height: 48
                placeholderText: qsTr("请输入文件名")
            }
            ColumnLayout {
                height: 48
                Layout.fillWidth: true
                Text {
                    id: notify
                    text: qsTr("正在采集")
                    opacity: 0
                }
                Text {
                    id: curtime
                    text: "00:00"
                }
            }
            Button {
                id: recBtn
                text: qsTr("开始采集")
                background: Rectangle {
                    color: "green"
                    id: bkd
                }
                onClicked:  {
                    if(filename.text === "") {
                        fnotify.open()
                    }else if(cnt_timer.running) {
                        cnt_timer.running = false
                        bkd.color = "grey"
                        notify.opacity = 0

                    }else {
                        if(bkd.colr !== "grey") {
                            cnt_timer.running = true
                            notify.opacity = 1
                            recBtn.text = qsTr("结束采集")
                        }
                    }
                }
                NotifyPopup {
                    id: fnotify
                    text: qsTr("请输入文件名")
                }
            }

            RowLayout {
                Button {
                    text: qsTr("取消")
                    onClicked: {
                        addBCFilePopup.close()
                    }
                    background: Rectangle {
                        color: "grey"
                    }
                }

                Item {
                    Layout.fillWidth: true
                }
                Button {
                    background: Rectangle {
                        color: "green"
                    }
                    text: qsTr("添加文件")
                    onClicked: {
                        if(filename.text === "") {
                            fnotify.open();
                        }else if(cnt_timer.seconds>0){
                            Services.addBroadCastFileName(filename.text);
                            bcfview.model = Services.getBroadcastFileNames();
                            addBCFilePopup.close();
                        }
                    }
                }
            }
            Timer {
                id: cnt_timer
                interval: 1000
                running: false
                repeat: true
                property int seconds: 0
                onTriggered: {
                    var mi, si, mt, st
                    seconds++
                    mi = parseInt(seconds/60)
                    si = seconds%60
                    mt = mi<10 ?"0"+mi: ""+mi
                    st = si<10 ?"0"+si: ""+si
                    curtime.text = mt + ":" + st

                }
            }
        }


    }

}
